import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Component, ElementRef, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import * as XLSX from 'xlsx';
import { ImportedFileService } from '../imported-file.service';
import { Importedfile, DesignEntity } from '../imported-file';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-imported-files-list',
  templateUrl: './imported-files-list.component.html',
  styleUrls: ['./imported-files-list.component.css']
})
export class ImportedfilesListComponent implements OnInit {

  importedfiles$: DesignEntity[];
  selectedId: number;
  inputdata = [];
  directRoute: string;
  screenId:string;
  designTypeId: string;
  screenIdList: string[] = [];
  selectedValue = null;

  constructor(
    private service: ImportedFileService,
    private route: ActivatedRoute,
    private el: ElementRef
  ) { }

  ngOnInit() {
    this.screenId = sessionStorage.getItem("screenId");
    this.designTypeId = sessionStorage.getItem("designTypeId");

    if (this.screenId != null ) {
      this.service.getImportedfileList(this.screenId, this.designTypeId).subscribe(
        data => {this.importedfiles$ = data; 
          if(this.importedfiles$.length === 0){
            return;
          }
        }
      );
    }

    this.service.getScreenIdList().subscribe(
      data => {this.screenIdList = data;
        if(this.screenIdList.length === 0){
          return;
        }
      }
    );
  }

  onSearch() {
    sessionStorage.setItem('screenId',this.screenId);
    sessionStorage.setItem('designTypeId',this.designTypeId);

    this.service.getImportedfileList(this.screenId, this.designTypeId).subscribe(
      data => {this.importedfiles$ = data; 
        if(this.importedfiles$.length === 0){
          return;
        }
      }
    );

  }

  goBack() {
    // TODO
  }
}
